#include  <stdio.h>
#include  <iostream>
#include  <vector>
#include  <algorithm>
using namespace std;

struct entry
{
	string	dna;
	int		sum;
};

bool cmp(struct entry *a, struct entry *b)
{
	return a->sum < b->sum;
}

int main(int argc, char *argv[])
{

	int a,b;
	scanf("%d %d",&a,&b);

	vector<struct entry*> arr;
	struct entry *p ;

	for( int i = 0; i < b ; i++ )
	{
		p = new struct entry;
		cin >> p->dna;
		p->sum = 0;
		for( int i  = 0; i < a-1 ; i++ )
		{
			int j = i+1;
			while( j < a )
			{
				if(p->dna[i] > p->dna[j])
					p->sum++;

				j++;
			}
		}
		arr.push_back(p);		
	}
	
	stable_sort(arr.begin(),arr.end(),cmp);
	
	for( int i = 0; i < arr.size() ; i++ )
	{
		cout << arr[i]->dna  << endl;
	}
	
}
